package nachos.threads;

public class Adult extends WorldStateListener implements Runnable {

   public Adult() {
      super();
   }

   /**
    * allow adult to cross to other island
    */
   public void run() {
      while( shouldRun() ) {
         //critical section start
         if( island==OAHU 
               && Boat.side() == OAHU 
               && childOnMolokai() ) {
            Boat.condlock.acquire();
            while( Boat.condstate == false ) {
               Boat.condition.sleep();
            }
			Boat.condition.wakeAll();
            Boat.condlock.release();
            cross();
            Boat.condlock.acquire();
            Boat.condstate = true;
            Boat.condition.wakeAll();
            Boat.condlock.release();
            KThread.currentThread().yield();
         }
         //critical section end
         KThread.currentThread().yield();
      }
   }

   /**
    * move boat to other island and allow adult to get off
    */
   public void cross() {
      super.cross();
      if (island == OAHU) {
         Boat.bg.AdultRowToMolokai();
         island = MOLOKAI;
      }
      else {
         Boat.bg.AdultRowToOahu();
         island = OAHU;
      }
   }

}

